//
//  ViewController.m
//  TomCat
//
//  Created by Kangli Yang on 2023/12/17.
//

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imgTom;
- (IBAction)onClickAnimation:(id)sender;


- (void) startAnimationWithFilePrefixName:(NSString *)prefixFileName andFileCount:(int) fileCount;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}




- (IBAction)onClickAnimation:(id)sender
{
    switch ([sender tag])
    {
            // 喝牛奶
        case 0:
            [self startAnimationWithFilePrefixName:@"drink" andFileCount:81];
            break;
            // 放屁
        case 1:
            [self startAnimationWithFilePrefixName:@"fart" andFileCount:28];
            break;
            // 猫抓
        case 2:
            [self startAnimationWithFilePrefixName:@"scratch" andFileCount:56];
            break;
            // 吃小鸟
        case 3:
            [self startAnimationWithFilePrefixName:@"eat" andFileCount:40];
            break;
            // 敲锣
        case 4:
            [self startAnimationWithFilePrefixName:@"cymbal" andFileCount:13];
            break;
            // 扔蛋糕
        case 5:
            [self startAnimationWithFilePrefixName:@"pie" andFileCount:24];
            break;
            // 左脚疼
        case 6:
            [self startAnimationWithFilePrefixName:@"foot_right" andFileCount:30];
            break;
            // 右脚疼
        case 7:
            [self startAnimationWithFilePrefixName:@"foot_left" andFileCount:30];
            break;
            // 肚子疼
        case 8:
            [self startAnimationWithFilePrefixName:@"stomach" andFileCount:34];
            break;
            // 敲脑袋
        case 9:
            [self startAnimationWithFilePrefixName:@"knockout" andFileCount:81];
            break;
            
        default:
            break;
    }
}

/**
 * 开始播放动画
 */
- (void) startAnimationWithFilePrefixName:(NSString *)prefixFileName andFileCount:(int) fileCount
{
    // 如果当前正在播放动画，则点击无效
    if([[self imgTom] isAnimating])
    {
        return;
    }
    // 创建数组，用于存放动画的每一帧图片
    NSMutableArray *arrayM = [NSMutableArray array];
    
    // 使用循环加载每一帧图片，并且将其存入arrayM中
    for(int i=0; i<fileCount; i++)
    {
        NSString *fileName = [NSString stringWithFormat:@"%@_%02d.jpg", prefixFileName, i];
        NSBundle *mainBundle = [NSBundle mainBundle];
        NSString *filePath = [NSString stringWithFormat:@"%@/%@/%@", [mainBundle bundlePath], prefixFileName, fileName];
        UIImage *image = [UIImage imageWithContentsOfFile:filePath];
        [arrayM addObject:image];
    }
    // 将帧动画数组赋值给UIImageView的帧动画数组属性
    [[self imgTom] setAnimationImages:arrayM];
    // 设置动画播放时间
    [[self imgTom] setAnimationDuration:fileCount*0.05F];
    // 设置动画播放次数
    [[self imgTom] setAnimationRepeatCount:1];
    // 开始播放动画
    [[self imgTom] startAnimating];
    // 动画播放完毕，清空缓存
    [[self imgTom] performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:fileCount*0.05F];
    
}
@end
